#!/usr/bin/python
import unittest
import tempfile
import sys
import os
import shutil
from StringIO import StringIO

# Import app modules
tests_dir = os.path.dirname(sys.argv[0])
from uya import routerconf
import common
from uya.routerconf.configmod.configobj import validate


expected_modified_filenames = [
    '/etc/hostname', 
    '/etc/hosts',
    '/etc/resolv.conf',
    '/etc/default/ntpdate',
    '/etc/network/interfaces',
    '/etc/hostapd/hostapd-ath1.conf',
    '/etc/wpa_supplicant/wpasupplicant-ath2.conf',
    '/etc/network/nat-static-routes',
    '/etc/quagga/daemons',
    '/etc/quagga/ospfd.conf',
    '/etc/quagga/zebra.conf',    
]

expected_commands = [
    ['hostname', 'dell'],
    ['/etc/init.d/networking', 'restart'],
    ['/etc/init.d/quagga', 'restart'],
]

class TestRouterConfiguration(unittest.TestCase, common.BaseTestConfig):
    root_tests_dir = tests_dir
    config_dir = "test/fixtures"        
        
    def test_run_config(self):
        def callback(config): 
            return routerconf.run_config(config, run_commands=False)        
        self.compare_filesystems(expected_modified_filenames, 
            expected_commands, callback)

    def test_get_config(self):
        config = common.init_filesystems_for_get(tests_dir)
        template_file = os.path.join(self.config_dir, "uya.template.conf")
        self.assertTrue(routerconf.get_config(config, template_file))
        # TODO: Test all
        #self.assertEqual("originalhostname", config["general"]["hostname"])
                    
                
def suite():
    return unittest.TestLoader().loadTestsFromTestCase(TestRouterConfiguration)
                 
if __name__ == '__main__':
    unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))
